//
//  AnimationViewController.m
//  DotAnimation
//
//  Created by ShiMac on 15/6/11.
//  Copyright (c) 2015年 guoyan. All rights reserved.
//

#import "AnimationViewController.h"

@interface Indicator : UIView

-(void)startAnimation;

@end



@implementation Indicator


-(void)startAnimation{
    
    CGFloat CircleSize=self.frame.size.width*0.6;
    
    NSTimeInterval beginTime=CACurrentMediaTime();
    
    for(NSInteger i=0;i<2;i++)
    {
        
        CALayer *layer=[CALayer layer];
        layer.frame=CGRectMake((self.frame.size.width-CircleSize)*i, 0, CircleSize, CircleSize);
        layer.backgroundColor=[UIColor colorWithRed:237/255.0f green:85/255.0f blue:101/255.0f alpha:1.0f].CGColor;
        layer.cornerRadius=CircleSize/2;
        

        CAKeyframeAnimation *keyAnimation=[CAKeyframeAnimation animationWithKeyPath:@"transform"];
        keyAnimation.removedOnCompletion=NO;
        keyAnimation.repeatCount=100;
        keyAnimation.duration=1.8f;
        keyAnimation.values=@[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 0)]];
        
        keyAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        


        keyAnimation.beginTime =beginTime- (keyAnimation.duration / 2.0f * i);

        
        [layer addAnimation:keyAnimation forKey:@"animaton"];
        
        [self.layer addSublayer:layer];
        
    }
    
    
    
    
    
    
}

@end





@interface AnimationViewController ()

@end

@implementation AnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=[UIColor whiteColor];
    Indicator *indictor=[[Indicator alloc] initWithFrame:CGRectMake((self.view.frame.size.width-30)/2, (self.view.frame.size.height-30)/2, 30, 30)];
    
    
    [self.view addSubview:indictor];
    
    
    [indictor startAnimation];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
